package com.wf.netty.coder.encoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;

/**
 * 功能描述
 *
 * @author wf
 * @since 2022/5/26
 */
public class Integer2ByteEncoderTester {
    @Test
    public void testInteger2Byte() {
        ChannelInitializer<EmbeddedChannel> channelInitializer = new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel ch) throws Exception {
                ch.pipeline().addLast(Integer2ByteEncoder.INSTANCE);
            }
        };
        EmbeddedChannel channel = new EmbeddedChannel(channelInitializer);
        for (int j = 0; j < 100; j++) {
            channel.write(j);  //向通道写入整数
        }
        channel.flush();
        //取得通道的出站数据包
        ByteBuf buf = (ByteBuf) channel.readOutbound();
        while (null != buf) {
            System.out.println("o = " + buf.readInt());
            buf = (ByteBuf) channel.readOutbound();
        }

    }
}
